/*静态配置库操作
Created on 2020年05月10日
@author: zxyong 13738196011
 */

package com.zxy.db_Self;

import com.zxy.adminlog.UsAdmin_Log;
import com.zxy.common.Com_Para;
import com.zxy.db1.DBManager1;
import com.zxy.model.T_PROC_NAME;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Db_Common_Self
{
	public StringBuilder			sqlException	= new StringBuilder();
	public ResultSet				rs				= null;
	public PooledConnection_Self	conn_a			= null;

	// / <summary>
	// / 开始事务
	// / </summary>
	public PooledConnection_Self BeginTransaction() throws SQLException
	{
		this.conn_a = DBManager_Self.getConnection();
		this.conn_a.BeginTranSaction();
		return this.conn_a;
	}

	// / <summary>
	// / 执行查询sql语句
	// / </summary>
	public ResultSet Common_Sql(StringBuilder strSql)
	{
		ResultSet rs = null;
		try
		{
			this.conn_a = DBManager_Self.getConnection();
			rs = this.conn_a.executeQuery(strSql);
		}
		catch(SQLException e)
		{
			StringBuilder sb = new StringBuilder();
			sb.append("数据库操作出错[strSql]" + strSql.toString() + "\r\n" + e.getMessage());
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,sb);
			try
			{
				uL.WriteLog();
			}
			catch(IOException localIOException)
			{
			}
			return this.rs;
		}
		catch(Exception es)
		{
			es.printStackTrace();
		}
		finally
		{
			this.conn_a.returnConnection();
		}
		return rs;
	}

	public ResultSet Common_Sql(PooledConnection_Self conn,StringBuilder strSql) throws SQLException
	{
		ResultSet rs = null;
		try
		{
			rs = conn.executeQuery(strSql);
		}
		catch(SQLException e)
		{
			StringBuilder sb = new StringBuilder();
			sb.append("数据库操作出错[strSql]" + strSql.toString() + "\r\n" + e.getMessage());
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,sb);
			try
			{
				uL.WriteLog();
			}
			catch(IOException localIOException)
			{
			}
			conn.RollBackTranSaction();
			return rs;
		}
		return rs;
	}

	public int CommonExec_SqlNoCon(PooledConnection_Self conn,StringBuilder strSql)
	{
		int iResult = -1;
		try
		{
			iResult = conn.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			iResult = -1;
			StringBuilder sb = new StringBuilder();
			sb.append("数据库操作出错[strSql]" + strSql.toString() + "\r\n" + e.getMessage());
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,sb);
			try
			{
				uL.WriteLog();
			}
			catch(IOException localIOException)
			{
			}
		}

		return iResult;
	}

	public int CommonExec_Sql(StringBuilder strSql)
	{
		int iResult = -1;
		try
		{
			this.conn_a = DBManager_Self.getConnection();
			iResult = this.conn_a.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			iResult = -1;
			StringBuilder sb = new StringBuilder();
			sb.append("数据库操作出错[strSql]" + strSql.toString() + "\r\n" + e.getMessage());
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,sb);
			try
			{
				uL.WriteLog();
			}
			catch(IOException localIOException)
			{
			}
		}
		finally
		{
			this.conn_a.returnConnection();
		}
		return iResult;
	}

	public int CommonExec_Sql(PooledConnection_Self conn,StringBuilder strSql) throws SQLException
	{
		int iResult = -1;
		try
		{
			iResult = conn.executeUpdate(strSql);
		}
		catch(SQLException e)
		{
			iResult = -1;
			StringBuilder sb = new StringBuilder();
			sb.append("数据库操作出错[strSql]" + strSql.toString() + "\r\n" + e.getMessage());
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,sb);
			try
			{
				uL.WriteLog();
			}
			catch(IOException localIOException)
			{
			}
			conn.RollBackTranSaction();
		}
		return iResult;
	}

	public int GetMax_Main_Id(PooledConnection_Self conn,String Table_Name) throws SQLException
	{
		ResultSet rs = null;
		int iResult = 1;
		StringBuilder strSql = new StringBuilder();
		try
		{
			strSql.append("select TB_KEY from T_TB_KEY where TB_NAME = '" + Table_Name.toUpperCase() + "'");
			rs = Common_Sql(conn,strSql);

			if(rs.next())
			{
				iResult = rs.getInt("TB_KEY") + 1;
				strSql.delete(0,strSql.length());
				strSql.append("update T_TB_KEY set TB_KEY = '" + String.valueOf(iResult) + "' where TB_NAME='" + Table_Name.toUpperCase() + "'");
			}
			else
			{
				iResult = 1;
				strSql.delete(0,strSql.length());
				strSql.append("insert into T_TB_KEY values('" + Table_Name.toUpperCase() + "','1')");
			}
			CommonExec_Sql(conn,strSql);
		}
		catch(Exception en)
		{
			StringBuilder sb = new StringBuilder();
			sb.append("数据库操作出错[strSql]" + strSql.toString() + "\r\n" + en.getMessage());
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,sb);

			try
			{
				uL.WriteLog();
			}
			catch(IOException e)
			{
			}
			conn.RollBackTranSaction();
			iResult = 0;
		}
		finally
		{
			rs.close();
		}
		return iResult;
	}

	/*
	 * ProName 存储过程名 Parameters输入参数 ParamTypes参数类型String Int float Date
	 * ParamOutName输出参数名 ParamOutType输出参数类型
	 */
	public ResultSet Common_Sql_Proc(PooledConnection_Self conn,String ProName,String[] Parameters,String[] ParamTypes,String[] ParamOutName,String[] ParamOutType,T_PROC_NAME trn)
	{
		try
		{
			if(trn.INF_TYPE.equals("1"))
				rs = conn.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
			else rs = conn.ParamExecuteQuery(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType,new StringBuilder(trn.INF_EN_SQL));
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + ProName + " " + e.getMessage());
			com.zxy.adminlog.UsAdmin_Log uL = new com.zxy.adminlog.UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("数据库操作出错[strSql]" + ProName + "\r\n" + e.getMessage()));
			try
			{
				uL.WriteLog();
			}
			catch(IOException e1)
			{
				e1.printStackTrace();
			}
			try
			{
				conn.RollBackTranSaction();
			}
			catch(SQLException e1)
			{
			}
		}
		return rs;
	}

	/*
	 * ProName 存储过程名 Parameters输入参数 ParamTypes参数类型String Int float Date
	 * ParamOutName输出参数名 ParamOutType输出参数类型
	 */
	public ResultSet Common_Sql_Proc(String ProName,String[] Parameters,String[] ParamTypes,String[] ParamOutName,String[] ParamOutType,T_PROC_NAME trn)
	{
		try
		{
			conn_a = DBManager_Self.getConnection();
			if(trn.INF_TYPE.equals("1"))
				rs = conn_a.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
			else rs = conn_a.ParamExecuteQuery(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType,new StringBuilder(trn.INF_EN_SQL));
		}
		catch(SQLException e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + ProName + " " + e.getMessage());
			com.zxy.adminlog.UsAdmin_Log uL = new com.zxy.adminlog.UsAdmin_Log(Com_Para.ApplicationPath,new StringBuilder("数据库操作出错[strSql]" + ProName + "\r\n" + e.getMessage()));
			try
			{
				uL.WriteLog();
			}
			catch(IOException e1)
			{
				e1.printStackTrace();
			}
			return rs;
		}
		catch(Exception e)
		{
			sqlException.delete(0,sqlException.length());
			sqlException.append("数据库操作出错请查看程序错误日志文件:" + ProName + " " + e.getMessage());
		}
		finally
		{
			conn_a.returnConnection();
		}
		return rs;
	}

	public synchronized ResultSet Common_Sql_Proc(String ProName,String[] Parameters,String[] ParamTypes,String[] ParamOutName,String[] ParamOutType) throws SQLException
	{
		try
		{
			this.conn_a = DBManager_Self.getConnection();
			this.rs = this.conn_a.executeProcedure(ProName,Parameters,ParamTypes,ParamOutName,ParamOutType);
		}
		catch(SQLException e)
		{
			StringBuilder sb = new StringBuilder();
			sb.append("数据库存储过程操作出错[strSql]" + ProName + "\r\n" + e.getMessage());
			UsAdmin_Log uL = new UsAdmin_Log(Com_Para.ApplicationPath,sb);
			try
			{
				uL.WriteLog();
			}
			catch(IOException localIOException)
			{
			}
			return this.rs;
		}
		catch(Exception localException)
		{
		}
		finally
		{
			this.conn_a.returnConnection();
		}
		return this.rs;
	}
}